package com.nip.workmanager.common.utilities;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.nip.workmanager.common.ConstantDatabase;

import android.content.Context;
import android.util.Log;

public class UtilitiesDataBase {

	/**
	 * Create the folder
	 * 
	 * @param root
	 *            dir root
	 * @param folderName
	 *            folder create
	 * @return
	 */
	public static boolean CreateFolder(File root, String folderName) {

		if (root != null) {

			File file = new File(root, folderName);

			if (!file.exists()) {
				if (file.mkdir()) {
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}

		} else {
			return false;
		}
	}

	/**
	 * Copy file from assets to dir
	 * 
	 * @param context
	 *            Context
	 * @param pathDir
	 *            Path to Dir
	 * @param fileName
	 *            Name of file
	 * @throws IOException
	 */
	public static Boolean CopyFileToDir(Context context, String pathDir,
			String fileName) throws IOException {
		InputStream inputStream = context.getAssets().open(fileName);
		String fullName = pathDir + fileName;
		OutputStream outputStream = new FileOutputStream(fullName);
		byte[] buffer = new byte[1024];
		int count;
		while ((count = inputStream.read(buffer)) > 0) {
			outputStream.write(buffer, 0, count);
		}
		outputStream.flush();
		outputStream.close();
		inputStream.close();
		return true;
	}

	/**
	 * Copy file from assert to system dir
	 * 
	 * @param context
	 * @param fileName
	 * @throws IOException
	 */
	public static Boolean CopyFileToDir(Context context, String fileName)
			throws IOException {
		InputStream inputStream = context.getAssets().open(fileName);
		String pathDir = "/data/data/" + context.getPackageName();
		Log.v("CopyFileToDir", pathDir);
		File file = new File(pathDir);
		if (!CreateFolder(file, "databases")) {
			return false;
		}
		String fullName = pathDir + "/databases/" + fileName;
		Log.v("CopyFileToDir","Full path: " + fullName);
		OutputStream outputStream = new FileOutputStream(fullName);
		byte[] buffer = new byte[1024];
		int count;
		while ((count = inputStream.read(buffer)) > 0) {
			outputStream.write(buffer, 0, count);
		}
		outputStream.flush();
		outputStream.close();
		inputStream.close();
		Log.v("CopyFileToDir","Copy database success!");
		return true;
	}

	/**
	 * Check file have existed yet
	 * 
	 * @param pathDir
	 * @param fileName
	 * @return
	 */
	public static Boolean IsDataBaseExisted(String pathDir, String fileName) {
		File file = new File(pathDir + fileName);
		return file.exists();
	}

	/**
	 * Check file have existed in system dir
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static Boolean IsDataBaseExisted(Context context, String fileName)
			throws IOException {
		String fullName = "/data/data/" + context.getPackageName()
				+ "/databases/" + fileName;
		Log.v("IsDataBaseExisted", fullName);
		File file = new File(fullName);
		return file.exists();
	}
	
	/**
	 * If database have not existed, That will copy database
	 */
	public static void CopyDataBase(Context context) {
		try {
			if (!UtilitiesDataBase.IsDataBaseExisted(context,
					ConstantDatabase.DATABASE_NAME)) {
				UtilitiesDataBase.CopyFileToDir(context,
						ConstantDatabase.DATABASE_NAME);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("Error", e.getMessage());
		}
	}
}
